#include "sprite_manager.h"

// Default constructor for class Spritemanager
SpriteManager::SpriteManager()
{
	sf::Sprite* placeholder = new sf::Sprite;
	placeholder->SetColor(sf::Color(255, 0, 0));
	placeholder->SetPosition(0, 0);
	placeholder->SetCenter(placeholder->GetSize() / 2.f);
	SpriteMeta placeholder_meta = {"$placeholder"};
	
	this->sprites.push_back(placeholder);
	this->sprites_meta.push_back(placeholder_meta);
}

// Destructor
SpriteManager::~SpriteManager()
{
	for(std::vector<sf::Sprite*>::iterator iterator = sprites.begin();
		iterator != sprites.end(); ++iterator)
	{
		delete *iterator;
	}
}

// function set_sprite()
//  - Sets sprite using image
//  - Returns true if succeeds and false if fails
bool SpriteManager::set_sprite(const sf::Image *image, const float position_x, const float position_y, const std::string identifier)
{
	sf::Sprite* new_sprite = new sf::Sprite;
	new_sprite->SetImage(*image);
	new_sprite->SetPosition(position_x, position_y);
	new_sprite->SetCenter(new_sprite->GetSize() / 2.f);
	
	SpriteMeta new_sprite_meta = {identifier};
	
	this->sprites.push_back(new_sprite);
	this->sprites_meta.push_back(new_sprite_meta);
	
	return true;
}

// function get_sprite()
//  - Recieves identifier as argument
//  - Returns a pointer to a sprite
sf::Sprite* SpriteManager::get_sprite(const std::string identifier) const
{
	int sprite_key = 0;
	int loop_count = 0; // variable to count loop, will refer to image_key
	for(std::vector<SpriteMeta>::const_iterator iterator = sprites_meta.begin();
		iterator != sprites_meta.end(); ++iterator, ++loop_count)
	{
		if( (*iterator).identifier == identifier )
		{
			sprite_key = loop_count;
			break;
		}
	}
	
	return this->sprites.at(sprite_key);
}

// function draw_all_sprites()
//  - Draws all sprites to window
std::vector<sf::Sprite*> SpriteManager::get_all_sprites() const{
	return this->sprites;
}